package cn.rtplay.esp32camcar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.content.Intent;
import android.os.Bundle;
import android.widget.SeekBar;

import cn.rtplay.esp32camcar.databinding.ActivitySettingBinding;
import cn.rtplay.esp32camcar.myView.IndicatorSeekBar;
import cn.rtplay.esp32camcar.toolUtils.LogUtil;

public class SettingActivity extends AppCompatActivity {
    private static final String TAG = "SettingActivity";
    public static final String BROADCAST_USER_SETTING = "userSetting";
    public static final String BROADCAST_SETTING_KEY = "settingKey";
    public static final String BROADCAST_SETTING_VALUE = "settingValue";

    private ActivitySettingBinding mViewBinding;
    private LocalBroadcastManager localBroadcastManager;
    private UserData mUserSetting;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewBinding = ActivitySettingBinding.inflate(getLayoutInflater());
        setContentView(mViewBinding.getRoot());

        localBroadcastManager = LocalBroadcastManager.getInstance(this);

        mUserSetting = new UserData(this);
        setBackground(mUserSetting.getUserSetting(UserData.PREFERENCES_SETTING_UI_COLOR));
        // 方向控制方式
        if (mUserSetting.getUserSetting(UserData.PREFERENCES_SETTING_DIRECTION_CONTROL_MODE) == UserData.VALUE_DIRECTION_CONTROL_MODE_SENSOR) {
            mViewBinding.dcmSensor.setChecked(true);
        } else {
            mViewBinding.dcmManual.setChecked(true);
        }
        mViewBinding.directionControlMode.setOnCheckedChangeListener((radioGroup, i) -> {
            if (i == mViewBinding.dcmSensor.getId()) {
                mUserSetting.setUserSetting(UserData.PREFERENCES_SETTING_DIRECTION_CONTROL_MODE, UserData.VALUE_DIRECTION_CONTROL_MODE_SENSOR);
            } else {
                mUserSetting.setUserSetting(UserData.PREFERENCES_SETTING_DIRECTION_CONTROL_MODE, UserData.VALUE_DIRECTION_CONTROL_MODE_MANUAL);
            }
            Intent intent = new Intent(BROADCAST_USER_SETTING);
            intent.putExtra(BROADCAST_SETTING_KEY, UserData.PREFERENCES_SETTING_DIRECTION_CONTROL_MODE);
            intent.putExtra(BROADCAST_SETTING_VALUE, mUserSetting.getUserSetting(UserData.PREFERENCES_SETTING_DIRECTION_CONTROL_MODE));
            localBroadcastManager.sendBroadcast(intent);
        });
        // 方向盘样式
        if (mUserSetting.getUserSetting(UserData.PREFERENCES_SETTING_STEERING_WHEEL_STYLE) == UserData.VALUE_STEERING_WHEEL_STYLE_1) {
            mViewBinding.wheelStyle1.setChecked(true);
        } else {
            mViewBinding.wheelStyle2.setChecked(true);
        }
        mViewBinding.wheelStyle.setOnCheckedChangeListener((radioGroup, i) -> {
            if (i == mViewBinding.wheelStyle1.getId()) {
                mUserSetting.setUserSetting(UserData.PREFERENCES_SETTING_STEERING_WHEEL_STYLE, UserData.VALUE_STEERING_WHEEL_STYLE_1);
            } else {
                mUserSetting.setUserSetting(UserData.PREFERENCES_SETTING_STEERING_WHEEL_STYLE, UserData.VALUE_STEERING_WHEEL_STYLE_2);
            }
            Intent intent = new Intent(BROADCAST_USER_SETTING);
            intent.putExtra(BROADCAST_SETTING_KEY, UserData.PREFERENCES_SETTING_STEERING_WHEEL_STYLE);
            intent.putExtra(BROADCAST_SETTING_VALUE, mUserSetting.getUserSetting(UserData.PREFERENCES_SETTING_STEERING_WHEEL_STYLE));
            localBroadcastManager.sendBroadcast(intent);
        });
        // 档位控制方式
        if (mUserSetting.getUserSetting(UserData.PREFERENCES_SETTING_GEAR_CONTROL_MODE) == UserData.VALUE_GEAR_CONTROL_MODE_AUTO) {
            mViewBinding.controlModeAuto.setChecked(true);
        } else {
            mViewBinding.controlModeManual.setChecked(true);
        }
        mViewBinding.controlMode.setOnCheckedChangeListener((radioGroup, i) -> {
            if (i == mViewBinding.controlModeAuto.getId()) {
                mUserSetting.setUserSetting(UserData.PREFERENCES_SETTING_GEAR_CONTROL_MODE, UserData.VALUE_GEAR_CONTROL_MODE_AUTO);
            } else {
                mUserSetting.setUserSetting(UserData.PREFERENCES_SETTING_GEAR_CONTROL_MODE, UserData.VALUE_DIRECTION_CONTROL_MODE_MANUAL);
            }
            Intent intent = new Intent(BROADCAST_USER_SETTING);
            intent.putExtra(BROADCAST_SETTING_KEY, UserData.PREFERENCES_SETTING_GEAR_CONTROL_MODE);
            intent.putExtra(BROADCAST_SETTING_VALUE, mUserSetting.getUserSetting(UserData.PREFERENCES_SETTING_GEAR_CONTROL_MODE));
            localBroadcastManager.sendBroadcast(intent);
        });
        // 图像质量
        if (mUserSetting.getUserSetting(UserData.PREFERENCES_SETTING_IMAGE_QUALITY) == UserData.VALUE_IMAGE_QUALITY_HD) {
            mViewBinding.imageQualityHD.setChecked(true);
        } else if (mUserSetting.getUserSetting(UserData.PREFERENCES_SETTING_IMAGE_QUALITY) == UserData.VALUE_IMAGE_QUALITY_STANDARD) {
            mViewBinding.imageQualityStandard.setChecked(true);
        } else {
            mViewBinding.imageQualitySmooth.setChecked(true);
        }
        mViewBinding.imageQuality.setOnCheckedChangeListener((radioGroup, i) -> {
            if (i == mViewBinding.imageQualityHD.getId()) {
                mUserSetting.setUserSetting(UserData.PREFERENCES_SETTING_IMAGE_QUALITY, UserData.VALUE_IMAGE_QUALITY_HD);
            } else if (i == mViewBinding.imageQualityStandard.getId()) {
                mUserSetting.setUserSetting(UserData.PREFERENCES_SETTING_IMAGE_QUALITY, UserData.VALUE_IMAGE_QUALITY_STANDARD);
            } else {
                mUserSetting.setUserSetting(UserData.PREFERENCES_SETTING_IMAGE_QUALITY, UserData.VALUE_IMAGE_QUALITY_SMOOTH);
            }
            Intent intent = new Intent(BROADCAST_USER_SETTING);
            intent.putExtra(BROADCAST_SETTING_KEY, UserData.PREFERENCES_SETTING_IMAGE_QUALITY);
            intent.putExtra(BROADCAST_SETTING_VALUE, mUserSetting.getUserSetting(UserData.PREFERENCES_SETTING_IMAGE_QUALITY));
            localBroadcastManager.sendBroadcast(intent);
        });
        // 音效开关
        if (mUserSetting.getUserSetting(UserData.PREFERENCES_SETTING_SOUND) == UserData.VALUE_SOUND_OPEN) {
            mViewBinding.soundOpen.setChecked(true);
        } else {
            mViewBinding.soundClose.setChecked(true);
        }
        mViewBinding.soundSetting.setOnCheckedChangeListener((radioGroup, i) -> {
            if (i == mViewBinding.soundOpen.getId()) {
                mUserSetting.setUserSetting(UserData.PREFERENCES_SETTING_SOUND, UserData.VALUE_SOUND_OPEN);
            } else {
                mUserSetting.setUserSetting(UserData.PREFERENCES_SETTING_SOUND, UserData.VALUE_SOUND_CLOSE);
            }
            Intent intent = new Intent(BROADCAST_USER_SETTING);
            intent.putExtra(BROADCAST_SETTING_KEY, UserData.PREFERENCES_SETTING_SOUND);
            intent.putExtra(BROADCAST_SETTING_VALUE, mUserSetting.getUserSetting(UserData.PREFERENCES_SETTING_SOUND));
            localBroadcastManager.sendBroadcast(intent);
        });
        // 音量
        mViewBinding.volume.setProgress(mUserSetting.getUserSetting(UserData.PREFERENCES_SETTING_VOLUME));
        mViewBinding.volume.setOnSeekBarChangeListener(new IndicatorSeekBar.OnIndicatorSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, float indicatorOffset) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                LogUtil.d(TAG, "onStopTrackingTouch:" + seekBar.getProgress());
                mUserSetting.setUserSetting(UserData.PREFERENCES_SETTING_VOLUME, seekBar.getProgress());
                Intent intent = new Intent(BROADCAST_USER_SETTING);
                intent.putExtra(BROADCAST_SETTING_KEY, UserData.PREFERENCES_SETTING_VOLUME);
                intent.putExtra(BROADCAST_SETTING_VALUE, mUserSetting.getUserSetting(UserData.PREFERENCES_SETTING_VOLUME));
                localBroadcastManager.sendBroadcast(intent);
            }
        });
        // 操作界面背景色
        mViewBinding.uiColor.setOnCheckedChangeListener((radioGroup, i) -> {
            if (i == mViewBinding.uiBlack.getId()) {
                mUserSetting.setUserSetting(UserData.PREFERENCES_SETTING_UI_COLOR, UserData.VALUE_UI_COLOR_BLACK);
            } else if (i == mViewBinding.uiGrey.getId()) {
                mUserSetting.setUserSetting(UserData.PREFERENCES_SETTING_UI_COLOR, UserData.VALUE_UI_COLOR_GREY);
            } else if (i == mViewBinding.uiDarkslategray.getId()) {
                mUserSetting.setUserSetting(UserData.PREFERENCES_SETTING_UI_COLOR, UserData.VALUE_UI_COLOR_DARKSLATEGRAY);
            } else if (i == mViewBinding.uiMidnightblue.getId()) {
                mUserSetting.setUserSetting(UserData.PREFERENCES_SETTING_UI_COLOR, UserData.VALUE_UI_COLOR_MIDNIGHTBLUE);
            } else if (i == mViewBinding.uiSlateblue.getId()) {
                mUserSetting.setUserSetting(UserData.PREFERENCES_SETTING_UI_COLOR, UserData.VALUE_UI_COLOR_SLATEBLUE);
            } else if (i == mViewBinding.uiSienna.getId()) {
                mUserSetting.setUserSetting(UserData.PREFERENCES_SETTING_UI_COLOR, UserData.VALUE_UI_COLOR_SIENNA);
            }
            setBackground(mUserSetting.getUserSetting(UserData.PREFERENCES_SETTING_UI_COLOR));
            Intent intent = new Intent(BROADCAST_USER_SETTING);
            intent.putExtra(BROADCAST_SETTING_KEY, UserData.PREFERENCES_SETTING_UI_COLOR);
            intent.putExtra(BROADCAST_SETTING_VALUE, mUserSetting.getUserSetting(UserData.PREFERENCES_SETTING_UI_COLOR));
            localBroadcastManager.sendBroadcast(intent);
        });
        // 返回键
        mViewBinding.exit.setOnClickListener(view -> onBackPressed());
        // 角度校正
        mViewBinding.correction.setOnClickListener(view -> {
            Intent intentCorrectionActivity = new Intent(SettingActivity.this, CorrectionActivity.class);
            startActivity(intentCorrectionActivity);
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        overridePendingTransition(0, R.anim.right_out);
    }

    private void setBackground(int colorIndex) {
        switch (colorIndex) {
            case UserData.VALUE_UI_COLOR_GREY:
                mViewBinding.settingPager.setBackgroundColor(ContextCompat.getColor(this, R.color.grey));
                break;
            case UserData.VALUE_UI_COLOR_DARKSLATEGRAY:
                mViewBinding.settingPager.setBackgroundColor(ContextCompat.getColor(this, R.color.darkslategray));
                break;
            case UserData.VALUE_UI_COLOR_MIDNIGHTBLUE:
                mViewBinding.settingPager.setBackgroundColor(ContextCompat.getColor(this, R.color.midnightblue));
                break;
            case UserData.VALUE_UI_COLOR_SLATEBLUE:
                mViewBinding.settingPager.setBackgroundColor(ContextCompat.getColor(this, R.color.slateblue));
                break;
            case UserData.VALUE_UI_COLOR_SIENNA:
                mViewBinding.settingPager.setBackgroundColor(ContextCompat.getColor(this, R.color.sienna));
                break;
            case UserData.VALUE_UI_COLOR_BLACK:
            default:
                mViewBinding.settingPager.setBackgroundColor(ContextCompat.getColor(this, R.color.black));
        }
    }
}